<?php
/**
 * @文件名称: Request.php
 * @author: eddy
 * @Date: 2016-11-7
 * @Copyright: 2016 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\infrastructure\uri;
use Zend\Http\Client;
use think\Log;
class Request extends Client{
     /**
      * @param string $url  请求的接口url
      * @param array $param 请求的参数
      * @param string $type 请求的类型（没有类型默认是get请求 get传参 ）
      *               0： get请求 get传参
      *               1： get请求 post传参
      *               2： post请求 get传参
      *               3： post请求 post传参
      * @return array $value 返回值
      */
     public function getArrayData($url,$param=null,$type=0){
         $this->setUri($url);
         if(!empty($param)){
             if(empty($type) || $type == 0){
//                 $this->setParameterGet($param);
//                 $this->setHeaders([
//                     'Content-Type' => 'application/json',
//                     'authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
//                 ]);
//                 $this->setMethod('GET');
                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('GET')
                     ->setRawBody(json_encode($param));
                 $result = $this->getRequest();
             }
             elseif($type == 1){
                 //$this->setHeaders(array('Content-Type'=>'application/json'));
//                 $this->setParameterPost($param);
//                 $this->setHeaders([
//                     'Content-Type' => 'application/json',
//                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
//                 ]);
//                 $this->setMethod('GET');

                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('GET')
                     ->setRawBody(json_encode($param));
                 $result = $this->getRequest();
             }
             elseif($type == 2){
                 $this->setParameterGet($param);
                 $this->setMethod('POST');
                 $result = $this->getRequest();
             }
             elseif($type == 3){
//                 $this->setHeaders(array('Content-Type'=>'application/json'));

                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('POST')
                     ->setRawBody(json_encode($param));

//                 $this->setParameterPost($param);
//                 $this->setMethod('POST');
                 $result = $this->getRequest();
             }
             elseif($type == 4){
                 //$this->setHeaders(array('Content-Type'=>'application/json'));

                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('PUT')
                     ->setRawBody(json_encode($param));

//                 $this->setParameterPost($param);
//                 $this->setMethod('PUT');
                 $result = $this->getRequest();
             }
             elseif($type == 5){
                 $this->setParameterGet($param);
                 $this->setMethod('PUT');
                 $result = $this->getRequest();
             }
             elseif($type == 6){
                 $this->setParameterGet($param);
                 $this->setMethod('DELETE');
                 $result = $this->getRequest();
             }
             elseif($type == 7){
                 //$this->setHeaders(array('Content-Type'=>'application/json'));
                 $this->setParameterPost($param);
                 $this->setMethod('DELETE');
                 $result = $this->getRequest();
             }
             else{
                 echo '您输入的类型不正确';
                 exit;
             }
         }
         else{
             if($type == 0){
                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('GET');
                 $result = $this->getRequest();
             }
             else{
                 $this->setHeaders([
                     'Content-Type' => 'application/json',
                     'Authorization'=>"Basic Zmxhc2hDYXNoOjAzMDcwNTk0LWU5ZGQtNGQ3ZS04ZmQzLWJmNmJhMDE3MDM0NA=="
                 ])
                     ->setOptions(['sslverifypeer' => false])
                     ->setMethod('POST');
                 $result = $this->getRequest();
             }
         }
         $result = $this->send($result);
         $httpStatus = $result->getStatusCode();
         $array = array(
                 //'username' => $_SESSION['user']['username'],
                 //'userid' => $_SESSION['user']['userId'],
                 'interface'=>$url,
                 'params'=>$param,
                 'type'=>empty($type)?'get':$type,
         );
         if ($httpStatus != 200){
             $array['status'] = $httpStatus;
         }
         Log::write($array,'interface');
         $value = json_decode($result->getBody(),true);
         return $value;
     }
     /**
      * @param string $url  请求的接口url
      * @param array $param 请求的参数
      * @param string $type 请求的类型（没有类型默认是get请求 get传参 ）
      *               0： get请求 get传参0
      *               1： get请求post传参
      *               2： post请求get传参
      *               3： post请求post传参
      * @return json  返回值
      */
     public function getJsonData($url,$param,$type=0){
         $this->setUri($url);
         if(!empty($param)){
             if(empty($type) || $type == 0){
                 $this->setParameterGet($param);
                 $this->setMethod('GET');
                 $result = $this->getRequest();
             }
             elseif($type == 1){
                 $this->setHeaders(array('Content-Type'=>'application/json'));
                 $this->setParameterPost($param);
                 $this->setMethod('GET');
                 $result = $this->getRequest();
             }
             elseif($type == 2){
                 $this->setParameterGet($param);
                 $this->setMethod('POST');
                 $result = $this->getRequest();
             }
             elseif($type == 3){
                 $this->setHeaders(array('Content-Type'=>'application/json'));
                 $this->setParameterPost($param);
                 $this->setMethod('POST');
                 $result = $this->getRequest();
             }
             elseif($type == 4){
                 $this->setHeaders(array('Content-Type'=>'application/json'));
                 $this->setParameterPost($param);
                 $this->setMethod('PUT');
                 $result = $this->getRequest();
             }
             elseif($type == 5){
                 $this->setParameterGet($param);
                 $this->setMethod('PUT');
                 $result = $this->getRequest();
             }
             elseif($type == 6){
                 $this->setParameterGet($param);
                 $this->setMethod('DELETE');
                 $result = $this->getRequest();
             }
             elseif($type == 7){
                 $this->setHeaders(array('Content-Type'=>'application/json'));
                 $this->setParameterPost($param);
                 $this->setMethod('DELETE');
                 $result = $this->getRequest();
             }
             else{
                 echo '您输入的类型不正确';
                 exit;
             }
         }
         else{
             if($type == 0){
                 $this->setMethod('GET');
                 $result = $this->getRequest();
             }
             else{
                 $this->setMethod('POST');
                 $result = $this->getRequest();
             }
         }
         $result = $this->send($result);
         $httpStatus = $result->getStatusCode();
         $array = array(
                 //'username' => $_SESSION['user']['username'],
                 //'userid' => $_SESSION['user']['userId'],
                 'interface'=>$url,
                 'params'=>$param,
                 'type'=>empty($type)?'get':$type,
         );
         if ($httpStatus != 200){
             $array['status'] = $httpStatus;
         }
         Log::write($array,'interface');
         echo $result->getBody();
         exit;
     }
}